package com.shy.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LogAspect {
    // 切面方法 计时
    @Around("execution(public void com.shy.aop.UserService.*(..) )")
    public void log(ProceedingJoinPoint proceePoint) {
        long begin = System.currentTimeMillis();

        try {
            proceePoint.proceed();
        } catch (Throwable e) {
            System.out.println("异常信息：" + e.getMessage());
        }

        long end = System.currentTimeMillis();

        System.out.println("耗时：" + (end - begin) + "毫秒");

    }

    @Before("execution(public void com.shy.aop.UserService.*(..) )")
    public void logBefore(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();

        System.out.println("方法名：" + name + " 参数为： " + Arrays.toString(args));
    }

    @AfterThrowing(value = "execution(public void com.shy.aop.UserService.*(..) )", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Exception e) {
        System.out.println("异常信息1：" + e.getMessage());
    }
}
